package org.muninn.niagara.math;

import javax.baja.status.BStatusBoolean;
import javax.baja.status.BStatusNumeric;
import javax.baja.sys.BComponent;
import javax.baja.sys.BIcon;
import javax.baja.sys.Context;
import javax.baja.sys.Flags;
import javax.baja.sys.Property;
import javax.baja.sys.Sys;
import javax.baja.sys.Type;

public class BSetpointSelect extends BComponent

{
  /** Most general trigger **/
  public static final Property globalTrig = newProperty(Flags.SUMMARY, new BStatusBoolean(true));
  public void setGlobalTrig(BStatusBoolean v) { set("globalTrig", v); }
  public BStatusBoolean getGlobalTrig() { return (BStatusBoolean) get("globalTrig"); }
  
  /** Intermediate trigger **/
  public static final Property localTrig = newProperty(Flags.SUMMARY, new BStatusBoolean(false));
  public void setLocalTrig(BStatusBoolean v) { set("localTrig", v); }
  public BStatusBoolean getLocalTrig() { return (BStatusBoolean) get("localTrig"); }
  
  /** Most specific trigger **/
  public static final Property statTrig = newProperty(Flags.SUMMARY, new BStatusBoolean(false));
  public void setStatTrig(BStatusBoolean v) { set("statTrig", v); }
  public BStatusBoolean getStatTrig() { return (BStatusBoolean) get("statTrig"); }
  
  /** Basic value **/
  public static final Property globalValue = newProperty(Flags.SUMMARY, new BStatusNumeric());
  public void setGlobalValue(BStatusNumeric v) { set("globalValue", v); }
  public BStatusNumeric getGlobalValue() { return (BStatusNumeric) get("globalValue"); }
  
  /** Localized value **/
  public static final Property localValue = newProperty(Flags.SUMMARY, new BStatusNumeric());
  public void setLocalValue(BStatusNumeric v) { set("localValue", v); }
  public BStatusNumeric getLocalValue() { return (BStatusNumeric) get("localValue"); }
  
  /** Nan value to allow for individual adjustment **/
  public static final Property nanValue = newProperty(Flags.SUMMARY, new BStatusNumeric());
  public void setNanValue(BStatusNumeric v) { set("nanValue", v); }
  public BStatusNumeric getNanValue() { return (BStatusNumeric) get("nanValue"); }
  
  /** Output selected from values based on trigger **/
  public static final Property outputValue = newProperty(Flags.SUMMARY|Flags.READONLY, new BStatusNumeric());
  public void setOutputValue(BStatusNumeric v) { set("outputValue", v); }
  public BStatusNumeric getOutputValue() { return (BStatusNumeric) get("outputValue"); }

  private boolean gTrig = true;
  private boolean lTrig = false;
  private boolean sTrig = false;
  
  public void changed(Property p, Context cx)
  {
    if (p.equals(globalTrig) && getGlobalTrig().getValue())
    {
      gTrig = true;
      lTrig = false;
      sTrig = false;
      
      getLocalTrig().setValue(false);
      getStatTrig().setValue(false);
    }
    
    else if (p.equals(localTrig) && getLocalTrig().getValue())
    {
      gTrig = false;
      lTrig = true;
      sTrig = false;
      
      getGlobalTrig().setValue(false);
      getStatTrig().setValue(false);
    }
    
    else if (p.equals(statTrig) && getStatTrig().getValue())
    {
      gTrig = false;
      lTrig = false;
      sTrig = true;
      
      getGlobalTrig().setValue(false);
      getLocalTrig().setValue(false);
    }
    
    set();
  }
  
  public void set()
  {
    if (gTrig)
      getOutputValue().setValue(getGlobalValue().getValue());
    
    else if (lTrig)
      getOutputValue().setValue(getLocalValue().getValue());
    
    else if (sTrig)
      getOutputValue().setValue(getNanValue().getValue());
  }
  
  public BIcon getIcon() { return icon; }
  private static final BIcon icon = BIcon.make("local:|module://muninn/org/muninn/niagara/graphics/MuninnGroup.PNG");

  public static final Type TYPE = Sys.loadType(BSetpointSelect.class);
  public Type getType() { return TYPE; }
}
